//
//  SISSMyCooperationRequestViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/4.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMyCooperationRequestViewController.h"
#import "SISSMineHouseCooperationCell.h"
#import "SISSHouseCooperationModel.h"
#import "SISSNetworkingManager+MineCooperation.h"
#import "SISSChatViewController.h"

static NSString *myCooperationRequestHouseCell = @"myCooperationRequestHouseCell";
@interface SISSMyCooperationRequestViewController ()<UITableViewDelegate,UITableViewDataSource,SISSMineHouseCooperationCellDelegate>
@property (weak, nonatomic) IBOutlet SISSBaseTableView *tableView;

@property (weak, nonatomic) IBOutlet UIView *containerView;

/** 房源合作数组 */
@property (strong, nonatomic) NSArray<SISSHouseCooperationModel *> *houseCooperationArr;

@end

@implementation SISSMyCooperationRequestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self requestMyRequestHouseCooperationData];
    
    // 上拉刷新
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf requestMyRequestHouseCooperationData];
    }];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.houseCooperationArr.count];
    return self.houseCooperationArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SISSMineHouseCooperationCell *cell = [tableView dequeueReusableCellWithIdentifier:myCooperationRequestHouseCell forIndexPath:indexPath];
    
    cell.delegate = self;
    cell.houseCooperationModel = self.houseCooperationArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - SISSMineHouseCooperationCellDelegate
- (void)houseCooperationCell:(SISSMineHouseCooperationCell *)cell clickContactButton:(UIButton *)button {
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    SISSChatViewController *chatVC = [[SISSChatViewController alloc] initWithConversationChatter:self.houseCooperationArr[indexPath.row].imAccount conversationType:EMConversationTypeChat];
    SISSContactModel *model = [[SISSContactModel alloc] init];
    model.name = self.houseCooperationArr[indexPath.row].name;
    model.portrait = self.houseCooperationArr[indexPath.row].portrait;
    model.spId = self.houseCooperationArr[indexPath.row].spId;
    model.accountType = 1;
    chatVC.contactModel = model;
    chatVC.title = self.houseCooperationArr[indexPath.row].name;
    [self.navigationController pushViewController:chatVC animated:YES];
}

- (void)requestMyRequestHouseCooperationData {
    
    WeakSelf;
    [kNetworkManager requestMyRequestHouseCooperationWithsuccess:^(NSArray<SISSHouseCooperationModel *> *houseCooperations) {
        
        self.houseCooperationArr = houseCooperations;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
        
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    
    // tableView相关
    // 自动行高
    self.tableView.estimatedRowHeight = 188;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    // 去掉cell系统的线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"SISSMineHouseCooperationCell" bundle:nil] forCellReuseIdentifier:myCooperationRequestHouseCell];
    
}

#pragma mark - 点击segmentControl的响应事件
- (IBAction)segmentControlClickAction:(UISegmentedControl *)sender {
    
    self.containerView.hidden = !sender.selectedSegmentIndex;
    
    NSLog(@"selectedSegmentIndex:  %zd",sender.selectedSegmentIndex);
}

@end
